package cn.edu.ujs.coor.controller;

import cn.edu.ujs.coor.model.Message;
import cn.edu.ujs.coor.model.User;
import cn.edu.ujs.coor.request.IdRequest;
import cn.edu.ujs.coor.request.message.MessagePageRequest;
import cn.edu.ujs.coor.response.JsonResponse;
import cn.edu.ujs.coor.response.PageResponse;
import cn.edu.ujs.coor.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 消息相关Controller
 *
 * @author 柳曾雄
 * @date 2018-07-29
 */
@RestController
@RequestMapping(value = "/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @RequestMapping(value = "/list")
    public JsonResponse list (MessagePageRequest request, User user) {
        request.setUserId(user.getId());
        PageResponse<Message> messages = messageService.page(request);
        return JsonResponse.success(messages);
    }

    @RequestMapping(value = "/info")
    public JsonResponse info (@RequestBody @Valid IdRequest request) {
        Message message = messageService.findById(request.getId());
        messageService.read(request.getId());
        return JsonResponse.success(message);
    }

    @RequestMapping(value = "/delete")
    public JsonResponse delete (@RequestBody @Valid IdRequest request) {
        messageService.deleteById(request.getId());
        return JsonResponse.success();
    }
}
